﻿/// <reference path="Z:\SoProject\LiberUI/Scripts/jquery-2.0.3.js" />
/// <reference path="Z:\SoProject\LiberUI/Scripts/LiberUI.Base.js" />

function AjaxHelper(mode, url, data, onEnd,state) {
	/// <summary>
	/// 创建一个Ajax请求代理【会自动处理需要登录验证的情况】
	/// </summary>
	/// <param name="mode">请求模式get、post、getJSON</param>
	/// <param name="url">请求的后台地址</param>
	/// <param name="data">要发生给后台的参数</param>
	/// <param name="onEnd">请求成功完成后的回调</param>
    /// <param name="state">回调时携带的状态对象</param>
    var job = {
        Type: mode === 'post' || mode === 'get'? mode : 'get',
        Url:url.toString(),
        Data: $.isPlainObject(data) ? data : null,
        Callback: ObjHelper.IsFunc(onEnd) ? onEnd : null,
        State:state
    }

    function onAjaxSuccess(rtn) {
        if (job.Callback) {
            job.Callback(rtn, job.State);
        }
        dispose();
    };

    function onAjaxErr(rtn) {
        if (rtn.status === 401) {
            new LoginAgent().ForEnd(loginEnd);
        }
        else{
            //TODO:使用标准的Ajax错误提示
            alert('Ajax请求失败，此处应该为标准提示组件');
            if (job.Callback) {
                job.Callback(rtn, job.State);
            }
        }
        dispose();
    };

    function loginEnd(rtn) {
        //登录成功则再次执行原先的ajax请求
        $.ajax(job.Url, { type: job.Type, data: job.Data, dataType: 'json', success: onAjaxSuccess, error: onAjaxErr });
    };

    function dispose() {
        job.State = null; //先解除对外部对象的引用，避免导致外部对象被错误删除掉
        job.Data = null;
        ObjHelper.DisposeObj(job);
    }

    $.ajax(job.Url, { type: job.Type, data: job.Data, dataType: 'json', success: onAjaxSuccess, error: onAjaxErr });
};

function LoginAgent() {
    if (document._loginAgent) {
        //检查是否已有一个登录代理已经在执行
        return document._loginAgent;
    }
    document._loginAgent = this;
    var fns = []; //登录完毕后待执行的回调
    var m = $("body").Unmask().Mask();

    this.ForEnd = function (_fn) {
        if (ObjHelper.IsFunc(_fn)) {
            fns.push(_fn);
        }
    };

    this.End = function (rtn) {
    	/// <summary>
    	/// 登录完成后执行的回调
    	/// </summary>
        /// <param name="rtn"></param>
        m.remove();
        for (var i = 0; i < fns.length; i++) {
            var f = fns[i];
            f(rtn);
        }
        fns = [];
        return this;
    };
    m.load("/SoProj/AjaxLogin");

    return this;
}

//#region 定义各种业务对象的界面描述模型
function TaskModel() {
    Model.apply(this);
    var instance = this;
    var titles = {};
    titles.SubSystem = '关联子系统';
    titles.Status = '执行状态';
    titles.Approve = '审批状态';
    titles.Content = '任务内容';
    titles.TimeCost = '实际耗时';
    titles.TimeValue = '可结算工作时';
    titles.Proposer = '提出人';
    titles.Charger = '责任人';
    titles.ProposeTime = '接入时间';
    titles.Deadline = '完成时限';
    titles.DL = '完成时限';
    titles.CreateTime = '登记时间';
    titles.UpdateTime = '最后更新时间';
    titles.Type = '任务类型';
    titles.Importance = '重急程度';

    instance.member.vf_getTypeName = function () {
        return 'ETask';
    };
    instance.member.vf_getTitle = function (_field) {
        return titles[_field];
    };
    instance.member.vf_convert = function (_field, _value) {
        var v = "";
        switch (_field) {
            case "Status":
                switch (_value) {
                    case "Active":
                        v = "处理中";
                        break;
                    case "Completed":
                        v = "已完成";
                        break;
                    case "Pending":
                        v = "暂停";
                        break;
                    case "Draft":
                        v = "未明确";
                        break;
                    case "Aborted":
                        v = "取消";
                        break;
                }
        }
        if (instance.originalObj.Approve && instance.originalObj.Approve == 'Approving')
        {
            v=v+'(审批中)'
        }
        return v;
    };
    instance.member.vf_actionFlag = function (_action) {
        switch (_action) {
            case "Complete":
                if (instance.currentObj.Status == 'Active') {
                    return true;
                }
                else {
                    return false;
                }
                break;
            case "Cancel":
                if (instance.currentObj.Status == 'Active') {
                    return true;
                }
                else {
                    return false;
                }
                break;
            case "Abandon":
                if (instance.currentObj.Status == 'Active') {
                    return true;
                }
                else {
                    return false;
                }
                break;
            case "Take":
                if (instance.currentObj.State == 'Draft') {
                    return true;
                }
                else {
                    return false;
                }
                break;
        }
    };
    this.constructor = TaskModel;
    return this;
};
TaskModel.prototype = Model.prototype;
//#endregion